<?php
require_once(dirname(__FILE__)."/../include/common.inc.php");
if(empty($dopost)) $dopost = '';
$aid = ( isset($aid) && is_numeric($aid) ) ? $aid : 0;
$type=empty($type)? "" : HtmlReplace($type,1);

if($aid==0)
{
	if($myset=='ajax')die('0');
	ShowMsg('文档id不能为空!','javascript:window.close();');
	exit();
}

require_once(DEDEINC."/memberlogin.class.php");
$ml = new MemberLogin();

if($ml->M_ID==0)
{
	if($myset=='ajax')die('1');
	ShowMsg('只有会员才允许收藏操作！','javascript:window.close();');
	exit();
}

$money = $dsql->GetOne("Select needmoney From `#@__addonsoft` where aid='$aid'");
$money = $money['needmoney'];

//读取文档信息
$arcRow = GetOneArchive($aid);
if($arcRow['aid']=='')
{
	if($myset=='ajax')die('2');
	ShowMsg("无法收藏未知文档!","javascript:window.close();");
	exit();
}
extract($arcRow, EXTR_SKIP);
$title = HtmlReplace($title,1);
$aid = intval($aid);
$addtime = time();

//检查所收藏的文档是否已存在
$jc = $dsql->GetOne("Select aid From `#@__member_stow` where aid='$aid' And mid='{$ml->M_ID}'");
if($jc)
{
	if($myset=='ajax')die('3');
	ShowMsg("该文档在您的收藏夹中已存在，请勿重复收藏!","javascript:window.close();");
	exit();
}

if($dopost=="cancel")
    {
		
		if($myset=='ajax')die('0');
		
	}



if($type==''){
	$row = $dsql->GetOne("Select * From `#@__member_stow` where aid='$aid' And mid='{$ml->M_ID}' AND type='' ");
	if(!is_array($row))
	{
		$dsql->ExecuteNoneQuery("INSERT INTO `#@__member_stow`(mid,aid,money,addtime) VALUES ('".$ml->M_ID."','$aid','$money','$addtime'); ");
  }
}else{
  $row = $dsql->GetOne("Select * From `#@__member_stow` where type='$type' and (aid='$aid' And mid='{$ml->M_ID}')");
  if(!is_array($row)){
  	$dsql->ExecuteNoneQuery(" INSERT INTO `#@__member_stow`(mid,aid,money,addtime,type) VALUES ('".$ml->M_ID."','$aid','$money','$addtime','$type'); ");
  }
}

//更新用户统计
$stows = $dsql->ExecuteNoneQuery2("UPDATE `#@__addonsoft` SET stows = stows+1 WHERE aid='$aid' ");
$row = $dsql->GetOne("SELECT COUNT(*) AS nums FROM `#@__member_stow` WHERE `mid`='{$ml->M_ID}' ");
$dsql->ExecuteNoneQuery("UPDATE #@__member_tj SET `stow`='$row[nums]' WHERE `mid`='".$ml->M_ID."'");


//更新会员动态
$feedtitle = "$title";
$feedtypename = "收藏了";
$dsql->ExecuteNoneQuery(" INSERT INTO `#@__member_feed`(mid,userid,uname,type,typename,aid,dtime,title,note,ischeck,sta) VALUES ('".$ml->M_ID."','".$ml->M_LoginID."','".$ml->M_UserName."','stows','$feedtypename','$aid','".time()."','$feedtitle','','1','0'); ");
if($myset=='ajax')die('4');
ShowMsg('成功收藏一篇文档！','javascript:window.close();');
?>